//
//  UserMainVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/26.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "UserMainVC.h"
#import "UserRegisterVC.h"

@interface UserMainVC ()

@property(weak, nonatomic) IBOutlet UIButton *backBt;
@property(weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property(strong, nonatomic) UIImageView *headerIv;
@property(strong, nonatomic) UILabel     *phoneLb;
@property(strong, nonatomic) UILabel     *versionLb;
@end

@implementation UserMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self removeGesture];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTab" object:@"0"];
    }];
    
    float width = [UIScreen mainScreen].bounds.size.width-72;
    float height = width*322/561;
    float originY = 0;
    float length = height/2;
    self.headerIv = [[UIImageView alloc] initWithFrame:CGRectMake(width/2-length/2, length/4, length, length)];
    [self.headerIv setImage:[UIImage imageNamed:@"img_set_user"]];
    [self.scrollView addSubview:self.headerIv];
    
    
    self.phoneLb = [[UILabel alloc] initWithFrame:CGRectMake(0, height*3/4, width, 20)];
    self.phoneLb.text = @"182****250";
    self.phoneLb.textColor = [UIColor whiteColor];
    self.phoneLb.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.phoneLb];
    
    originY +=height;
    float margin1 = 12;
    float margin2 = 6;
    float h = 45;
//    float topMargin[] = {margin1, margin2, margin2, margin2, margin1};
    float topMargin[] = {margin1, margin2, margin2, margin1};
    NSString *titles[] = {@"添加新设备",@"修改密码",@"APP版本",@"退出登录"};
//    NSString *titles[] = {@"添加新设备",@"修改密码",@"用户协议",@"APP版本",@"退出登录"};
    for (int i=0; i<4; i++) {
        originY += topMargin[i];
        UIButton *bt = [[UIButton alloc] initWithFrame:CGRectMake(0, originY, width, h)];
        [bt setBackgroundColor:[UIColor whiteColor]];
        bt.layer.cornerRadius = globalRadius;
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, h)];
        lb.text = titles[i];
        [lb setFont:[UIFont systemFontOfSize:14]];
        lb.textColor = colorWithString(@"#4f525b");
        [bt addSubview:lb];
        float imgH = 12;
        float imgW = imgH*20/42;
        if (i==2) {
            self.versionLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width-10, h)];
            self.versionLb.textAlignment = NSTextAlignmentRight;
            self.versionLb.font = [UIFont systemFontOfSize:14];
            self.versionLb.textColor = colorWithString(@"#8d8d8d");
            self.versionLb.text = [NSString stringWithFormat:@"V%@", appVersion()];
            [bt addSubview:self.versionLb];
        } else {
            UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(width-imgW-10, h/2-imgH/2, imgW, imgH)];
            [iv setImage:[UIImage imageNamed:@"img_jt_1"]];
            [bt addSubview:iv];
        }
        [[bt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            switch (i) {
                case 0: {
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"DeviceBindVCID"];
                    [self.navigationController pushViewController:vc animated:YES];
                    break;
                }
                case 1: {
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
                    UserRegisterVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"UserRegisterVCID"];
                    [vc setType:@"重置密码"];
                    [self.navigationController pushViewController:vc animated:YES];
                    break;
                }
                case 2: {
                    NSString *url = @"http://itunes.apple.com/lookup?id=1399525705";
                    [[VersionUtil shareInstance] isNeedUpdate:url block:^(BOOL isNeedUpdate, NSError *error) {
                        if (!error) {
                            if (isNeedUpdate) {
                                NSString *msg = @"\n\n有新版本可以升级啦，去appstore更新吧！\n\n";
                                UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
                                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//                                UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//
//                                }];
                                [vc addAction:cancel];
//                                [vc addAction:confirm];
                                [self presentViewController:vc animated:YES completion:nil];
                            } else {
                                [JPToast showText:@"当前已是最新版本"];
                            }
                        } else {
                            [JPToast showText:@"网络错误"];
                        }
                    }];
                    break;
                }
                case 3: {
                    NSString *msg = @"\n\n您确定要退出当前账号吗？\n\n";
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                        [[UserManager shareInstance] logout];
                        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
                        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"UserLoginVCID"];
                        [self.navigationController pushViewController:vc animated:YES];
                    }];
                    [vc addAction:cancel];
                    [vc addAction:confirm];
                    [self presentViewController:vc animated:YES completion:nil];
                    break;
                }
                default:
                    break;
            }
        }];
        [self.scrollView addSubview:bt];
        originY += h;
    }
//    CGSize size = self.scrollView.frame.size;
//    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, size.height-30, size.width, 30)];
//    lb.text = @"应用中的商业活动均与apple Inc.无关";
//    lb.textAlignment = NSTextAlignmentCenter;
//    [lb setFont:[UIFont systemFontOfSize:10]];
//    lb.textColor = [UIColor whiteColor];
//    [self.scrollView addSubview:lb];
//    for (int i=0; i<5; i++) {
//        originY += topMargin[i];
//        UIButton *bt = [[UIButton alloc] initWithFrame:CGRectMake(0, originY, width, h)];
//        [bt setBackgroundColor:[UIColor whiteColor]];
//        bt.layer.cornerRadius = globalRadius;
//        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, h)];
//        lb.text = titles[i];
//        [lb setFont:[UIFont systemFontOfSize:14]];
//        lb.textColor = colorWithString(@"#4f525b");
//        [bt addSubview:lb];
//        float imgH = 12;
//        float imgW = imgH*20/42;
//        if (i==3) {
//            self.versionLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width-10, h)];
//            self.versionLb.textAlignment = NSTextAlignmentRight;
//            self.versionLb.font = [UIFont systemFontOfSize:14];
//            self.versionLb.textColor = colorWithString(@"#8d8d8d");
//            self.versionLb.text = [NSString stringWithFormat:@"V%@", appVersion()];
//            [bt addSubview:self.versionLb];
//        } else {
//            UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(width-imgW-10, h/2-imgH/2, imgW, imgH)];
//            [iv setImage:[UIImage imageNamed:@"img_jt_1"]];
//            [bt addSubview:iv];
//        }
//        [[bt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
//            switch (i) {
//                case 0: {
//                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"DeviceBindGuideVCID"];
//                    [self.navigationController pushViewController:vc animated:YES];
//                    break;
//                }
//                case 1: {
//                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
//                    UserRegisterVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"UserRegisterVCID"];
//                    [vc setType:@"重置密码"];
//                    [self.navigationController pushViewController:vc animated:YES];
//                    break;
//                }
//                case 2: {
//                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
//                    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"UserProtocolVCID"];
//                    [self.navigationController pushViewController:vc animated:YES];
//                    break;
//                }
//                case 3: {
//                    NSString *url = @"http://itunes.apple.com/lookup?id=1399525705";
//                    [[VersionUtil shareInstance] isNeedUpdate:url block:^(BOOL isNeedUpdate, NSError *error) {
//                        if (!error) {
//                            if (isNeedUpdate) {
//                                NSString *msg = @"\n\n有新版本可以升级啦，去appstore更新吧！\n\n";
//                                UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
//                                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
//                                UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//
//                                }];
//                                [vc addAction:cancel];
//                                [vc addAction:confirm];
//                                [self presentViewController:vc animated:YES completion:nil];
//                            } else {
//                                [JPToast showText:@"当前已是最新版本"];
//                            }
//                        } else {
//                            [JPToast showText:@"网络错误"];
//                        }
//                    }];
//                    break;
//                }
//                case 4: {
//                    NSString *msg = @"\n\n您确定要退出当前账号吗？\n\n";
//                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
//                    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//                    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//                        [[UserManager shareInstance] logout];
//                        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
//                        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"UserLoginVCID"];
//                        [self.navigationController pushViewController:vc animated:YES];
//                    }];
//                    [vc addAction:cancel];
//                    [vc addAction:confirm];
//                    [self presentViewController:vc animated:YES completion:nil];
//                    break;
//                }
//                default:
//                    break;
//            }
//        }];
//        [self.scrollView addSubview:bt];
//        originY += h;
//    }
    [self.scrollView setContentSize:CGSizeMake(0, originY)];
}

- (void)updateView {
    User *user = [UserManager shareInstance].user;
    if (!user) {
        return;
    }
    if (self.phoneLb) {
        NSString *phone = [user.phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        self.phoneLb.text = phone;
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
